package cscie160.hw6;
import java.util.LinkedList;
/**
 * This class is responsible for working with requests queue.
 * Calls wait() if the queue is empty; otherwise retrieves the
 * ATMRunnable from the queue and executes the request contained there.
 * @author Michael Melnikov
*/
public class ATMThread extends Thread
{
    private LinkedList<ATMRunnable> requestQueue;

    public ATMThread (LinkedList<ATMRunnable> requestQueue)
    {
        super();
        this.requestQueue = requestQueue;
    }

    /**
     * manages all the functionality of the class
    */
    public void run()
    {
        ATMRunnable atm = null;
        while (true) {
            synchronized(requestQueue){
                while (requestQueue == null || requestQueue.isEmpty())
                {
                    try
                    {
                        requestQueue.wait();
                    }
                    catch (InterruptedException iex)
                    {
                        //System.out.println(getClass().getName() + " method run. "+iex.getMessage());
                    }
                }
                System.out.println(getClass().getName() + " method run. requestQueueSize="+
                    requestQueue.size() + " atm="+atm);
                atm = requestQueue.removeFirst(); 

                System.out.println(getClass().getName() + " method run. requestQueueSize="+
                    requestQueue.size() + " atm="+atm);
                try {
                    atm.run();
                }
                catch (RuntimeException e){}        
           }
       }
    }
}
